package com.itkjb.leisurely.resource.sys.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itkjb.leisurely.core.base.BaseConstants;
import com.itkjb.leisurely.core.base.BaseConstantsEnum;
import com.itkjb.leisurely.core.base.BaseDto;
import com.itkjb.leisurely.core.base.BaseResult;
import com.itkjb.leisurely.core.base.controller.BaseController;
import com.itkjb.leisurely.core.model.SearchModel;
import com.itkjb.leisurely.core.util.QueryUtil;
import com.itkjb.leisurely.resource.sys.model.SysDictModel;
import com.itkjb.leisurely.resource.sys.model.vo.SysDictVO;
import com.itkjb.leisurely.resource.sys.service.ISysDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.Serializable;
import java.util.List;
/**
 * <p>
 * 系统字典表 接口层
 * </p>
 *
 * @author itkjb.com
 * @since 2020-06-19
 */
@RestController
@RequestMapping("/sys/sys-dict")
@Api(value="系统字典表",tags={"系统字典表相关接口"})
public class SysDictController extends BaseController<SysDictModel,ISysDictService> {

    /**
     * 获取系统字典表列表
     * @param param 查询通用参数
     * @param model 特定条件参数
     * @param request
     * @param response
     * @return
     */
    @GetMapping
    @ApiOperation(value="获取系统字典表列表",notes="支持分页参数")
    @PreAuthorize("hasAnyAuthority('SysDict','SysDict:query') || hasRole('"+ BaseConstants.ADMINISTRATOR_ROLE_CODE +"') ")
    public BaseResult<IPage<SysDictModel>> page(BaseDto param, SysDictModel model, HttpServletRequest request, HttpServletResponse response){
        if(model.getDictType() == null){
            model.setDictType(0);
        }
        return super.page(param, model);
    }

    /**
     * 获取系统字典表列表
     * @param param 查询通用参数，分页参数不可用
     * @param model 特定条件参数
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value="获取系统字典表列表",notes="不支持分页")
    @PreAuthorize("hasAnyAuthority('SysDict','SysDict:query') || hasRole('"+ BaseConstants.ADMINISTRATOR_ROLE_CODE +"') ")
    public BaseResult<List<SysDictModel>> list(BaseDto param, SysDictModel model, HttpServletRequest request, HttpServletResponse response){
        BaseResult<List<SysDictModel>> res = new BaseResult<>();
        if(StringUtils.isBlank(model.getDictLabel()) &&  StringUtils.isBlank(model.getParentId())){
            res.setMsg("必须指定查询字典");
            return res;
        }
        return super.list(param, model);
        //return res;
    }

    /**
     * 公共字典查询，不需要权限
     * @param param
     * @param model
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/get")
    @ApiOperation(value="根据指定的字典标签获取字典详情",notes="")
    public BaseResult<List<SysDictVO>> get(BaseDto param, SysDictModel model, HttpServletRequest request, HttpServletResponse response){
        BaseResult<List<SysDictVO>> res = new BaseResult<>();
        if(StringUtils.isBlank(model.getDictLabel()) && StringUtils.isBlank(model.getParentId())){
            res.setMsg("必须指定查询字典");
            return res;
        }
        // 设置一下parentId，免得一次性查出所有子集
        if(StringUtils.isBlank(model.getParentId())){
            SysDictModel parent = new SysDictModel();
            parent.setDictType(0);
            parent.setDictLabel(model.getDictLabel());
            SearchModel<SysDictModel> searchModel= QueryUtil.build(param,parent);
            List<SysDictVO> parents = this.baseService.getDictDetail(searchModel);
            if(parents!=null && parents.size()>0){
                model.setParentId(parents.get(0).getId());
            }
        }
        // 设置查询可用的字典
        model.setStatus(1);
        SearchModel<SysDictModel> searchModel= QueryUtil.build(param,model);
        List<SysDictVO> data = this.baseService.getDictDetail(searchModel);
        res.toSuccess().setData(data);
        return res;
    }

    /**
     * 新增系统字典
     * @param param
     * @param request
     * @param response
     * @return
     */
    @PostMapping
    @ApiOperation(value="新增系统字典",notes="")
    @PreAuthorize("hasAnyAuthority('SysDict:add') || hasRole('"+ BaseConstants.ADMINISTRATOR_ROLE_CODE +"') ")
    public BaseResult<SysDictModel> add(@Valid @RequestBody SysDictModel param, HttpServletRequest request, HttpServletResponse response){
        BaseResult<SysDictModel> res = new BaseResult<>();
        // 新增字典标签
        if(param.getDictType() == null || param.getDictType() == 0){
            param.setKey("dict_label");
        }
        return super.add(param);
    }

    /**
     * 获取系统字典表详情
     * @param id
     * @param response
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value="获取系统字典表详情",notes="")
    @ApiParam(name = "id",type = "Serializable",required = true)
    @PreAuthorize("hasAnyAuthority('SysDict:info') || hasRole('"+ BaseConstants.ADMINISTRATOR_ROLE_CODE +"') ")
    public BaseResult<SysDictModel> info(@PathVariable("id") Serializable id, HttpServletResponse response){
        return super.info(id);
    }

    /**
     * 修改系统字典表
     * @param param
     * @param request
     * @param response
     * @return
     */
    @PutMapping
    @ApiOperation(value="修改系统字典表",notes="")
    @PreAuthorize("hasAnyAuthority('SysDict:edit') || hasRole('"+ BaseConstants.ADMINISTRATOR_ROLE_CODE +"') ")
    public BaseResult<SysDictModel> edit(@RequestBody SysDictModel param,HttpServletRequest request, HttpServletResponse response){
        if(param.isNeedUpdateSubLabel() && param.getDictType() == 0){
            BaseResult<SysDictModel> res ;
            SysDictModel oldModel = this.baseService.getById(param.getId());
            res = super.edit(param);
            if(res.getCode() == BaseConstantsEnum.HTTP_SUCCESS.getCode()){
                this.baseService.updateByLabel(oldModel.getDictLabel(), param.getDictLabel());
            }
            return res;
        }else{
            return super.edit(param);
        }

    }

    /**
     * 删除系统字典表
     * @param ids
     * @param request
     * @param response
     * @return
     */
    @DeleteMapping
    @ApiOperation(value="删除系统字典表",notes="注意参数是数字，就算单个，也要以数字的形式传递")
    @PreAuthorize("hasAnyAuthority('SysDict:del') || hasRole('"+ BaseConstants.ADMINISTRATOR_ROLE_CODE +"') ")
    public BaseResult<SysDictModel> delete(@RequestBody String[] ids, HttpServletRequest request, HttpServletResponse response){
        return super.delete(ids);
    }

}

